package selfProgram.StepOne;

import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;

public class StepOne {
    public static void main(String[] args) {
        File file = new File("C:\\Users\\zqzdidiannao\\Desktop\\codebaby\\softProject\\resources\\file.txt");
       Scanner scanner = null;

        try{
           scanner =new Scanner(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        HashMap<String, Integer> hashMap = new HashMap<String,Integer>();
        while (scanner.hasNextLine()){
            String line = scanner.nextLine();
            String[] lineWords=line.split("\\W+");
            //通过keyset方法得到所有的键，wordset表示表中存放单词的一列
            Set<String> wordSet=hashMap.keySet();
            for(int i=0;i<lineWords.length;i++)
            {
                //如果已经有这个单词了
                if(wordSet.contains(lineWords[i]))
                {
                    Integer number=hashMap.get(lineWords[i]);
                    number++;
                    hashMap.put(lineWords[i], number);
                }
                else
                {
                    hashMap.put(lineWords[i], 1);
                }
            }
        }
        System.out.println("统计单词：------------------------------");
        Iterator<String> iterator=hashMap.keySet().iterator();
        while(iterator.hasNext())
        {
            String word=iterator.next();
            System.out.println("单词： "+word+"出现次数："+hashMap.get(word));
        }
    }


}




